package com.service;

import com.alibaba.fastjson.JSON;
import com.entity.User_wzl;
import com.utils.EntityFactory;
import com.utils.ExecUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = "/user/queryUserList")
public class UserService_wzl extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (!req.getServletPath().equals("/user/queryUserList")) {
            resp.getWriter().write("403 not allowed");
            return;
        }
        String sql = "select * from userguanli_wzl";
        List<User_wzl> userWzlList = new ArrayList<>();
        List<Object> params = new ArrayList<>();
        Boolean judge = true;
        if (!req.getParameter("userphone").isEmpty()) {
            params.add(req.getParameter("userphone"));
            if (judge) {
                judge = false;
                sql = sql + " where";
            } else {
                sql = sql + " and";
            }
            sql = sql + " userphone like \"%\"?\"%\"";
        }
        if (!req.getParameter("username").isEmpty()) {
            params.add(req.getParameter("username"));
            if (judge) {
                judge = false;
                sql = sql + " where";
            } else {
                sql = sql + " and";
            }
            sql = sql + " username like \"%\"?\"%\"";
        }
        if (!req.getParameter("time1").isEmpty()) {
            params.add(req.getParameter("time1"));
            if (judge) {
                judge = false;
                sql = sql + " where";
            } else {
                sql = sql + " and";
            }
            sql = sql + " registertime >= ?";
        }
        if (!req.getParameter("time2").isEmpty()) {
            params.add(req.getParameter("time2"));
            if (judge) {
                judge = false;
                sql = sql + " where";
            } else {
                sql = sql + " and";
            }
            sql = sql + " registertime <= ?";
        }
        try (ResultSet resultSet = (ResultSet) ExecUtil.execute(sql, params, req, false)) {
            while (resultSet.next()) {
                EntityFactory entityFactory = new EntityFactory<>(User_wzl.class);
                User_wzl user_wzl = (User_wzl) entityFactory.startCombine(resultSet);
                userWzlList.add(user_wzl);
            }
            resp.getWriter().write(JSON.toJSONString(userWzlList));
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                ExecUtil.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

